<script setup>
import { ref } from 'vue'

const userInfo = ref({
    old_pwd: '',
    new_pwd: '',
    re_pwd: ''
})

const rules = {
    old_pwd: [  // 改为 old_pwd 以匹配字段名
        { required: true, message: '请输入用户原密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '密码必须是5-16位的非空字符串',
            trigger: 'blur'
        }
    ],
    new_pwd: [  // 改为 new_pwd 以匹配字段名
        { required: true, message: '请输入用户新密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '密码必须是5-16位的非空字符串',
            trigger: 'blur'
        },
        { 
            validator: (rule, value, callback) => {
                if (value === userInfo.value.old_pwd) {
                    callback(new Error('新密码不能与原密码相同'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    ],
    re_pwd: [  // 改为 re_pwd 以匹配字段名
        { required: true, message: '请确认用户新密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '密码必须是5-16位的非空字符串',
            trigger: 'blur'
        },
        { 
            validator: (rule, value, callback) => {
                if (value !== userInfo.value.new_pwd) {
                    callback(new Error('两次输入的密码不一致'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    ]
}

import { ElMessage } from 'element-plus'
import { userpasswordUpdateService } from '@/api/user.js'
const loading = ref(false)
import { useRouter } from 'vue-router'
const router = useRouter()

const updatePassword = async () => {
       try {
        loading.value = true
        const result = await userpasswordUpdateService(userInfo.value)
        ElMessage.success(result.msg ? result.msg : '密码修改成功')
        
        userInfo.value = { old_pwd: '', new_pwd: '', re_pwd: '' }
        
        // 修正路由跳转
        await router.push('/login')
        
        ElMessage({
            type: 'success',
            message: '请重新登录',
        })
    } catch (error) {
        ElMessage.error(error.message || '密码修改失败')
    } finally {
        loading.value = false // 确保无论如何都会关闭加载状态
    }

   
}
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="原密码" prop="old_pwd">  <!-- 改为 old_pwd -->
                        <el-input v-model="userInfo.old_pwd" type="password" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="new_pwd">  <!-- 改为 new_pwd -->
                        <el-input v-model="userInfo.new_pwd" type="password" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="re_pwd">  <!-- 改为 re_pwd -->
                        <el-input v-model="userInfo.re_pwd" type="password" show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updatePassword" :loading="loading">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>